import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;

public class ControlPanel extends JPanel {
	
	// the panel at the top of the GUI
	// deals with phone number
	// deals with sound schemes
	
	final int NUM_COLUMNS = 10;
	final int PHONE_NUMBER_LENGTH = 10;
	final int GUESS_SIZE = 4;
	final int NUM_COLORS = 4;
	final int NUMBER_SOUND_DIRECTORIES = 4;
	final String[] SOUND_DIRECTORIES = {"General", "Price is Right", "Simpsons", "Family Guy"};
	final int NUMBER_CARRIERS = 4;
	final String[] CARRIERS = {"AT&T", "Verizon", "T-Mobile", "Sprint / Nextel"};
	final int WINDOW_WIDTH = 651;
	final int CONTROL_PANEL_HEIGHT = 65;

	// all the gui components
	JButton startGame, submitGuess, restartGame;
	ButtonHandler buttonHandler;
	Game mastermindGame;
	StatusPanel statusPanel;
	GamePanel gamePanel;
	JTextField phoneNumber;
	JCheckBox notify;
	JComboBox phoneCombo, soundCombo;
	JLabel phoneLabel, carrierLabel, soundLabel;
	String sound, carrier;
	Sound startSequence, nextGuess, win, loss, notifySuccess, notifyFail;
	boolean error = false;

	public ControlPanel(GamePanel gamePanel) {

		// declare the buttons
		startGame = new JButton("START GAME");
		submitGuess = new JButton("SUBMIT GUESS");
		restartGame = new JButton("RESTART GAME");

		// phone number
		phoneNumber = new JTextField(10);
		phoneLabel = new JLabel("Phone: ");

		notify = new JCheckBox("Notify");
		
		phoneCombo = new JComboBox();
		carrierLabel = new JLabel("Carrier: ");
		
		soundCombo = new JComboBox();
		soundLabel = new JLabel("Sound Theme: ");
		
		this.setPreferredSize(new Dimension(WINDOW_WIDTH, CONTROL_PANEL_HEIGHT));
		
		for(int i = 0; i < NUMBER_SOUND_DIRECTORIES; i++)
			soundCombo.addItem(SOUND_DIRECTORIES[i]);
		
		for(int i = 0; i < NUMBER_CARRIERS; i++)
			phoneCombo.addItem(CARRIERS[i]);

		buttonHandler = new ButtonHandler();

		// Can only click these buttons after the game has been started
		submitGuess.setEnabled(false);
		restartGame.setEnabled(false);

		// Add Action Listeners
		startGame.addActionListener(buttonHandler);
		submitGuess.addActionListener(buttonHandler);
		restartGame.addActionListener(buttonHandler);

		// Add Elements
		this.add(phoneLabel);
		this.add(phoneNumber);
		this.add(carrierLabel);
		this.add(phoneCombo);
		this.add(notify);
		this.add(soundLabel);
		this.add(soundCombo);
		this.add(startGame);
		this.add(submitGuess);
		this.add(restartGame);
	}

	class ButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals("START GAME")) {
				// change the clickability of the buttons
				startGame.setEnabled(false);
				submitGuess.setEnabled(true);
				restartGame.setEnabled(true);
				phoneNumber.setEditable(false);
				phoneCombo.setEnabled(false);
				soundCombo.setEnabled(false);
				notify.setEnabled(false);
				
				// get the sound sequence
				sound = soundCombo.getSelectedItem().toString();
				// get the phone carrier
				carrier = phoneCombo.getSelectedItem().toString();
				
				// assign all the sounds from the sequence to the particular sound instances
				startSequence = new Sound("src/sounds/" + sound + "/startGame.wav");
				nextGuess = new Sound("src/sounds/" + sound + "/nextGuess.wav");
				win = new Sound("src/sounds/" + sound + "/win.wav");
				loss = new Sound("src/sounds/" + sound + "/lost.wav");
				notifyFail = new Sound("src/sounds/" + sound + "/notifyFail.wav");
				notifySuccess = new Sound("src/sounds/" + sound + "/notifySuccess.wav");
				
				// if run with cheat enabled, it will return the solution
				mastermindGame = new Game(NUM_COLUMNS, MastermindGUI.getCheat());
				startSequence.run();
				gamePanel = MastermindGUI.getGamePanel();
				statusPanel = MastermindGUI.getStatusPanel();
				gamePanel.setClickable((mastermindGame.getCurrentTurn() - 1),
						true);
				statusPanel.setStatus("Game in progress.  Current Guess: " + (mastermindGame.getCurrentTurn()));
			}
			if (e.getActionCommand().equals("SUBMIT GUESS")) {
				
				// check to see if there was a valid guess
				for (int i = 0; i < GUESS_SIZE; i++) {
					if (gamePanel
							.getValues(mastermindGame.getCurrentTurn() - 1)[i] < 1
							|| gamePanel.getValues(mastermindGame
									.getCurrentTurn() - 1)[i] > NUM_COLORS) {
						error = true;
						break;
					} else
						error = false;
				}
				if (error)
					JOptionPane.showMessageDialog(null,
							"The guess you enterd was invalid.");
				else {
					// store the guess
					int[] tempArray = mastermindGame.getResult(gamePanel
							.getValues((mastermindGame.getCurrentTurn() - 1)));
					gamePanel.setResult(mastermindGame.getCurrentTurn() - 2, tempArray);
					
					// if user did not guess properly after ten guesses, run lost
					if (mastermindGame.getLost()) {
						loss.run();
						int solution[] = mastermindGame.getSolutionArray();
						String solutionString = "";
						String solutionWord = "";
						for(int i = 0; i < solution.length; i++)
						{
							if(solution[i] == 1)
								solutionWord = "Red";
							else if(solution[i] == 2)
								solutionWord = "Yellow";
							else if(solution[i] == 3)
								solutionWord = "Blue";
							else if(solution[i] == 4)
								solutionWord = "Green";
							
							if(i == 0)
							{
								solutionString = solutionString + solutionWord;
							}
							else
								solutionString = solutionString + ", " + solutionWord;	
						}
						// end the game
						String output = "You failed to guess the correct pattern.  \nCorrect solution was: " + solutionString;
						statusPanel.setStatus(output);
						JOptionPane.showMessageDialog(null, output);
						submitGuess.setEnabled(false);
						restartGame.setEnabled(true);
						phoneNumber.setEditable(true);
						phoneCombo.setEnabled(true);
						soundCombo.setEnabled(true);
						notify.setEnabled(true);
						
						gamePanel.setClickable(mastermindGame.getCurrentTurn() - 2, false);
						
					// if the user wins
					} else if (mastermindGame.getWon()) {
						// still end the game
						submitGuess.setEnabled(false);
						restartGame.setEnabled(true);
						phoneNumber.setEditable(true);
						phoneCombo.setEnabled(true);
						soundCombo.setEnabled(true);
						notify.setEnabled(true);
						gamePanel.setClickable(mastermindGame.getCurrentTurn() - 2, false);
						
						// send out text message
						if (notify.isSelected())
							if (phoneNumber.getText().length() == PHONE_NUMBER_LENGTH) {
								SMS.sendSMS(phoneNumber.getText(), carrier);
								win.run();
								notifySuccess.run();
								String output = "You guessed the correct pattern in " + (mastermindGame.getCurrentTurn() - 1) + " turns.  \nNotification Successful";
								statusPanel.setStatus(output);
								JOptionPane
										.showMessageDialog(
												null, output);
							} else {
								// if the notification doesn't go properly
								win.run();
								notifyFail.run();
								String output = "You guessed the correct pattern in " + (mastermindGame.getCurrentTurn() - 1) + " turns.  \nNotification Failed - Invalid Phone Number";
								statusPanel.setStatus(output);
								JOptionPane
										.showMessageDialog(
												null, output);
												
							}
						else {
							// if user didnt input a phone number
							win.run();
							String output = "You guessed the correct pattern in " + (mastermindGame.getCurrentTurn() - 1) + " turns.";
							statusPanel.setStatus(output);
							JOptionPane
									.showMessageDialog(
											null, output);
						}
					} else {
						// go to the next turn
						gamePanel.setClickable((mastermindGame
								.getCurrentTurn() - 2), false);
						gamePanel.setClickable((mastermindGame
								.getCurrentTurn() - 1), true);
						gamePanel.setPegs(
								(mastermindGame.getCurrentTurn() - 1),
								gamePanel.getValues((mastermindGame
										.getCurrentTurn() - 2)));
						statusPanel.setStatus("Game in progress.  Current Guess: " + (mastermindGame.getCurrentTurn()));
						nextGuess.run();
					}
				}
			}
			if (e.getActionCommand().equals("RESTART GAME")) {
				gamePanel.clear();
				startGame.setEnabled(true);
				startGame.doClick();
			}
		}
	}
}